package com.ifast.hnzk.controller;


import java.util.Arrays;
import java.util.Date;

import com.ifast.hnzk.domain.LinksDO;
import com.ifast.hnzk.service.LinksService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ifast.common.annotation.Log;
import com.ifast.common.base.BaseController;
import com.ifast.common.utils.Result;

/**
 * 
 * <pre>
 * 
 * </pre>
 * <small> 2019-06-17 14:10:18 | wch</small>
 */
@Controller
@RequestMapping("/hnzk/links")
public class LinksController extends BaseController {
	@Autowired
	private LinksService linksService;
	
	@GetMapping()
	@RequiresPermissions("hnzk:links:links")
	String Links(){
	    return "hnzk/links/links";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("hnzk:links:links")
	public Result<Page<LinksDO>> list(LinksDO linksDTO){
        Wrapper<LinksDO> wrapper = new EntityWrapper<LinksDO>();
		wrapper.like("name",linksDTO.getName());
		wrapper.orderBy("id", false);
        Page<LinksDO> page = linksService.selectPage(getPage(LinksDO.class), wrapper);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("hnzk:links:add")
	String add(){
	    return "hnzk/links/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("hnzk:links:edit")
	String edit(@PathVariable("id") Long id,Model model){
		LinksDO links = linksService.selectById(id);
		model.addAttribute("links", links);
	    return "hnzk/links/edit";
	}
	
	@Log("添加")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("hnzk:links:add")
	public Result<String> save( LinksDO links){
		links.setCreatdate(new Date());
		linksService.insert(links);
        return Result.ok();
	}
	
	@Log("修改")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("hnzk:links:edit")
	public Result<String>  update( LinksDO links){
		boolean update = linksService.updateById(links);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log("删除")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("hnzk:links:remove")
	public Result<String>  remove( Long id){
		linksService.deleteById(id);
        return Result.ok();
	}
	
	@Log("批量删除")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("hnzk:links:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		linksService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
